package com.project.common.core.domain;

import com.project.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {
    private int code;
    private String msg;
    private T date;

    public static <T> R<T> ok() {
        return assembleResult(null, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return assembleResult(data, ResultCode.SUCCESS);
    }

    //默认错误码
    public static <T> R<T> fail() {
        return assembleResult(null, ResultCode.FAILED);
    }

    //指定错误码
    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(null, resultCode);
    }

    public static R<?> fail(int code, String msg) {
        return assembleResult(code,msg,null);
    }
    private static <T> R<T> assembleResult(T data, ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(r.getCode());
        r.setMsg(r.getMsg());
        r.setDate(data);
        return r;
    }

    private static <T> R<T> assembleResult(int code, String msg, T data) {
        R<T> r = new R<>();
        r.setCode(code );
        r.setMsg(msg);
        r.setDate(data);
        return r;
    }



}
